package com.be.beadmin.device.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.DaoTemplate;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteWorkbook;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.input.InspectPlanDto;
import com.be.beadmin.device.dto.input.InspectPlanNewDto;
import com.be.beadmin.device.dto.output.EhsInfoEx;
import com.be.beadmin.device.dto.output.EhsInfoExcelEx;
import com.be.beadmin.device.entity.EhsInfo;
import com.be.beadmin.device.repository.EhsInfoRepository;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.EhsInfoService;
import com.be.beadmin.device.dto.input.EhsInfoDto;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zty
* @date 2023-08-13
**/
@RestController
public class EhsInfoController {

    @Autowired
    private  EhsInfoService ehsInfoService;
    @Autowired
    private EhsInfoRepository ehsInfoRepository;


    @PostMapping(value = "/device/getEhsInfoByPage")
    public ResultModel getEhsInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(ehsInfoService.getEhsInfoByPage(pageEntity)) ;
    }

    @ApiOperation("生成巡检单")
    @PostMapping("/device/addEhsInfo")
    public ResultModel addEhsInfo(@RequestBody EhsInfoDto ehsInfoDto){
        ehsInfoService.addEhsInfo(ehsInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delEhsInfo")
    public ResultModel delEhsInfo(@RequestBody EhsInfoDto ehsInfoDto){
        ehsInfoService.delEhsInfo(ehsInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateEhsInfo")
    public ResultModel updateEhsInfo(@RequestBody EhsInfoDto ehsInfoDto){
        ehsInfoService.updateEhsInfo(ehsInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportEhsInfo")
    public void exportEhsInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<EhsInfoEx> ehsInfoPage = ehsInfoService.getEhsInfoByPage(pageEntity);
        List<EhsInfoEx> ehsInfoExes = ehsInfoPage.getContent();
        List<EhsInfoExcelEx> ehsInfoExcelExes=ehsInfoExes.stream().map(item->{
            EhsInfoExcelEx excel = new EhsInfoExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("EhsInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), EhsInfoExcelEx.class).sheet("sheet1").doWrite(ehsInfoExcelExes);
        ExcelWriterBuilder ewb =EasyExcel.write(response.getOutputStream(), EhsInfoExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(ehsInfoExcelExes);


    }

    @ApiOperation("获取EHS专员列表")
    @PostMapping("/device/getEhsCommissioners")
    public ResultModel getEhsCommissioners(){
        return ResultModel.success(ehsInfoService.getEhsCommissioners());
    }

    @ApiOperation("获取EHS处理人员列表")
    @PostMapping("/device/getEhsHandlers")
    public ResultModel getEhsHandlers(){
        return ResultModel.success(ehsInfoService.getEhsHandlers());
    }
}